package com.csi.controller;

import com.csi.domain.UserAssess;
import com.csi.service.AssessService;
import com.csi.utils.JsonResult;
import com.csi.utils.JwtUtil;
import com.csi.utils.ReturnResults;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("assess")
@Tag(name = "Assess", description = "评价相关接口")
public class AssessController {

    @Autowired
    private AssessService assessService;


    /**
     * 新增评价
     * @param userAssess
     * @param request
     * @return
     */
    @Operation(summary = "新增评价", description = "添加用户评价信息")
    @PostMapping("/save")
    public Object save(@Parameter(description = "用户评价对象", required = true) @RequestBody UserAssess userAssess,@Parameter(description = "HTTP请求对象", required = true) HttpServletRequest request){
        String token =  request.getHeader("token");
        long userId = JwtUtil.getTokenBody(token).get("userId",long.class);
        //测试
        /*String token = JwtUtil.getToken("zhangsan",1);
        long userId = JwtUtil.getTokenBody(token).get("userId",Long.class);*/

        userAssess.setUserId(userId);
        userAssess.setAccessDate(new Timestamp(System.currentTimeMillis()));
        int saved = assessService.save(userAssess);
        if(saved > 0){
            return new JsonResult<>();
        }
        return new JsonResult<>(ReturnResults.ACCESS_FAILED,"添加失败");
    }

    /**
     * 根据id查找评价
     * @param
     * @return
     */
    @Operation(summary = "查找评价", description = "根据id查找用户评价信息")
    @GetMapping("/findById")
    public Object findById(@Parameter(description = "HTTP请求对象", required = true) HttpServletRequest request){
        String token =  request.getHeader("token");
        long userId = JwtUtil.getTokenBody(token).get("userId",Long.class);
//        String token = JwtUtil.getToken("zhangsan",1);
//        long userId = JwtUtil.getTokenBody(token).get("userId",Long.class);
        Map<String,Object> assessList = assessService.findById(userId);
        if (!assessList.isEmpty()){
            return new JsonResult<>(assessList);
        }
        return new JsonResult<>(ReturnResults.ACCESS_FAILED,"查询失败");
    }

    /**
     * 根据套餐id查找评价
     * @return
     */
    @Operation(summary = "根据套餐id查找评价", description = "根据套餐id查找用户评价信息")
    @GetMapping("/findByPackageId")
    public Object findByPackageId(@RequestParam("packageId") long packageId){
        Map<String,Object> assessList =  assessService.findByPackageId(packageId);
        if (!assessList.isEmpty()){
            return new JsonResult<>(assessList);
        }
        return new JsonResult<>(ReturnResults.ACCESS_FAILED,"查询失败");
    }

}
